﻿using System;
using System.Windows.Forms;

namespace SunnyUI.Localization.Demo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SuspendLayout();
            //载入中文资源
            button3.PerformClick();
            ResumeLayout(false);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //传入工程文件（*.csproj）、程序文件（*.exe）
            FormGenerator.Load(
                @"C:\Users\Sunny\source\repos\SunnyUI.Localization\SunnyUI.Localization.Demo\SunnyUI.Localization.Demo.csproj",
                Application.ExecutablePath);

            //设置语言文件相对于程序文件的路径
            FormGenerator.LanguageDir = "Languages";

            //生成界面资源文件
            string rawFile = FormGenerator.Save();

            //将界面资源文件存储为相应语言的资源文件，待翻译（可多次生成，已有的不覆盖）
            FormGenerator.Save(rawFile, CultureInfos.SimplifiedChinese);
            FormGenerator.Save(rawFile, CultureInfos.English);

            //将代码资源文件存储为相应语言的资源文件，待翻译（可多次生成，已有的不覆盖）
            CodeTranslator.Current.Save(CultureInfos.SimplifiedChinese);
            CodeTranslator.Current.Save(CultureInfos.English);
        }

        private void LoadLanguage()
        {
            //翻译当前界面的语言资源
            FormTranslator.Translate(this, Global.CultureInfo);

            //翻译当前代码的语言资源
            CodeTranslator.Current.Translate(Global.CultureInfo);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //设置当前语言为简体中文
            Global.CultureInfo = CultureInfos.SimplifiedChinese;

            //载入语言资源
            LoadLanguage();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //设置当前语言为英文
            Global.CultureInfo = CultureInfos.English;

            //载入语言资源
            LoadLanguage();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            //打开语言资源文件目录，并选择中文语言资源
            Global.OpenDir(CultureInfos.SimplifiedChinese.LanguageFile());
        }

        private void button6_Click(object sender, EventArgs e)
        {
            //打开语言资源文件目录，并选择英文语言资源
            Global.OpenDir(CultureInfos.English.LanguageFile());
        }

        private void button1_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
            openFileDialog1.ShowDialog();
            saveFileDialog1.ShowDialog();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            Form2 frm = new Form2();
            //翻译当前界面的语言资源
            FormTranslator.Translate(frm, Global.CultureInfo);
            frm.ShowDialog();
            frm.Dispose();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show(CodeTranslator.Current.CloseInfo, CodeTranslator.Current.AskCaption, MessageBoxButtons.OKCancel) == DialogResult.Cancel)
            {
                e.Cancel = true;
            }
        }
    }
}
